-- 缴械打击

local HumanBone = TaleWorlds.Core.HumanBone
local MissionWeapon = TaleWorlds.MountAndBlade.MissionWeapon
local SkinVoiceManager = TaleWorlds.MountAndBlade.SkinVoiceManager
local EquipmentIndex = TaleWorlds.Core.EquipmentIndex

local onlyPlayer = true
local percent = 0.5 --缴械概率
local behavior

return {
onBeforeMissionBehaviorInitialize = function (mission)
    behavior = SubModule:CreateMissionBehavior({
        onAgentHit = function (affectedAgent, affectorAgent, affectorWeapon, blow, attackCollisionData)
            if blow.IsMissile or not affectedAgent.IsHuman or affectedAgent == affectorAgent or affectorAgent == nil or (onlyPlayer and not affectorAgent.IsMainAgent) then
                return
            end

            local eqIndex = affectedAgent:GetWieldedItemIndex(Agent.HandIndex.MainHand)
            if eqIndex:CompareTo(EquipmentIndex.WeaponItemBeginSlot) < 0 then
                return
            end

            local ww = affectedAgent.Equipment[eqIndex]
            if ww == MissionWeapon.Invalid then
                return
            end
            if MBRandom.RandomFloat > percent then
                return
            end
            if affectorAgent.IsMainAgent then
                InformationManager.DisplayMessage(InformationMessage("触发缴械！", Color(0.913, 0.505, 0.239)))
            end

            affectorAgent:MakeVoice(SkinVoiceManager.VoiceType.Yell, SkinVoiceManager.CombatVoiceNetworkPredictionType.NoPrediction)
            affectedAgent:HandleDropWeapon(false, eqIndex)

            SubModule:CreateParticleSystemAttachedToBone(affectedAgent.AgentVisuals, "psys_game_sparkle_a", HumanBone.HandR)
            mission:MakeSound(SoundEvent.GetEventIdFromString("event:/mission/combat/missile/high"), affectedAgent.Position, false, true, affectorAgent.Index, affectedAgent.Index)
            mission:AddSoundAlarmFactorToAgents(affectorAgent.Index, affectorAgent.Position, 15)
        end
    })
    mission:AddMissionBehavior(behavior)
end
}